package com.example.demo_01.service.impl;

import com.example.demo_01.mapper.CourseMapper;
import com.example.demo_01.service.CourseService;
import com.example.demo_01.util.ResultBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper CourseMapper;

    @Override
    public ResultBean findByParam(Map<String, String> param) {
        int pageNum = Integer.parseInt(param.get("pageNum"));
        int pageSize = Integer.parseInt(param.get("pageSize"));
        PageHelper.startPage(pageNum, pageSize);
        List<Map> list = CourseMapper.findByParam(param);
        if (list != null && list.size() > 0) {
            PageInfo<Map> pageInfo = new PageInfo<>(list);
            return ResultBean.getInstance().success().setData(pageInfo);
        }
        return ResultBean.getInstance().fail("查无数据");
    }

    @Override
    public ResultBean doSave(Map<String, String> param) {
        int row = CourseMapper.save(param);
        if (row > 0) {
            return ResultBean.getInstance().success("添加课程成功");
        }
        return ResultBean.getInstance().fail("添加课程失败");
    }

    @Override
    public ResultBean doUpdate(Map<String, String> param) {
        int row = CourseMapper.update(param);
        if (row > 0) {
            return ResultBean.getInstance().success("更新课程信息成功");
        }
        return ResultBean.getInstance().fail("更新课程信息失败");
    }

    @Override
    public ResultBean delete(String ids) {
        int row = CourseMapper.delete(ids);
        if (row > 0) {
            return ResultBean.getInstance().success("删除课程成功");
        }
        return ResultBean.getInstance().fail("删除课程失败");
    }
}
